/**
 * 项目名称（软件名称）：忠旺机械物资管理系统(mms)
 * 研发设计单位：忠旺机械设备制造有限公司机械设计院智能控制设计科设计与研发。
 * 版权所有：忠旺机械设备制造有限公司
 * 注：本项目的一切技术专利，使用规范归解释权由忠旺机械设备制造有限公司所有。
 * 软件版本：Ver 1.0.0
 * 文件路径：com.zhongwang.mms.module.purchase.controller
 * 创建时间：2020年03月16日 12时47分
 **/
package com.zhongwang.mms.module.jiaGongStorage.controller;

import com.alibaba.fastjson.JSON;
import com.zhongwang.mms.base.BaseController;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.gen.entity.PInventory;
import com.zhongwang.mms.gen.entity.Role;
import com.zhongwang.mms.gen.entity.TDept;
import com.zhongwang.mms.module.jiaGongStorage.model.JOutStorage;
import com.zhongwang.mms.module.jiaGongStorage.model.JStorageInventory;
import com.zhongwang.mms.module.jiaGongStorage.model.JStorageInventoryQueryModel;
import com.zhongwang.mms.module.jiaGongStorage.service.StorageOutService;
import com.zhongwang.mms.module.purchase.constant.PurchaseConstant;
import com.zhongwang.mms.module.purchase.model.PInventoryModel;
import com.zhongwang.mms.module.purchase.model.PInventoryQueryModel;
import com.zhongwang.mms.module.purchase.model.POutOrder;
import com.zhongwang.mms.module.purchase.model.PurWarOutManageParamModel;
import com.zhongwang.mms.module.purchase.service.PurWarOutManageService;
import com.zhongwang.mms.module.system.role.RoleService;
import com.zhongwang.mms.util.ShiroUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


/**
 * @ClassName： PurWarInManageController
 * @Description： 外购件领料出库控制层
 * @Author： 徐一贺
 * @Date： 2020/3/16 12:47
 * @Version： V1.0
 **/
@Controller
@RequestMapping("/jiaGongOut")
public class StorageOutController extends BaseController {

    @Autowired
    private PurWarOutManageService purWarOutManageService;
    @Autowired
    private StorageOutService storageOutService;
    @Autowired
    private RoleService roleService;

    /**
     * 领料出库页面
     *
     * @return 页面路径
     */
    @GetMapping("/page")
    public String PurWarOutManage(Model model) {
        //获取角色码用来进行控制页面显示内容
        List<Role> roles = new ArrayList<>();
        roles.addAll(roleService.findRolesByUserId(ShiroUtils.getUser().getId()));
        List<String> roleCodes = new ArrayList<>();
        for (Role role : roles) {
            roleCodes.add(role.getRoleCode());
        }
        if (ShiroUtils.getUser().getAccount().equals("admin")) {
            roleCodes.add("admin");
        }
        model.addAttribute("roleCodes", roleCodes);
        //获取制单人信息
        model.addAttribute("userName", ShiroUtils.getUser().getName());
        //获取出库日期
        model.addAttribute("currentDate", purWarOutManageService.getCurrentDate());
        return "jiaGongStorage/storageOut/page";
    }


    @PostMapping("/getWarOutOrderCode")
    @ResponseBody
    /**
     *@FunctionName: getOrderCode
     *@Description: 获取最新的出库单编号
     *@Author: 徐一贺
     *@CreateDate: 2020/3/23 7:55
     *@Phone: 18241927380
     *@Param: [param] 请求参数
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version: 1.0.0
     */
    public JsonResult getWarOutOrderCode(PurWarOutManageParamModel param) {
        //获取入库单单据编号
        String orderCode = purWarOutManageService.getOrderCode(PurchaseConstant.PCK);
        if (param.getOdrCode() != null && param.getOdrCode().equals(orderCode)) {
            return JsonResult.success("FAIL", orderCode);
        }
        return JsonResult.success("SUCCESS", orderCode);
    }


    @PostMapping("/findDeptList")
    @ResponseBody
    /**
     *@FunctionName: findDeptList
     *@Description: 获取部门列表
     *@Author: 徐一贺
     *@CreateDate: 2020/3/24 13:32
     *@Phone: 18241927380
     *@Param: []
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version: 1.0.0
     */
    public JsonResult findDeptList() {
        List<TDept> tDeptList = purWarOutManageService.findDeptList();
        return JsonResult.success("", tDeptList);
    }

    @PostMapping("/inventoryList")
    @ResponseBody
    /**
     *@FunctionName: findInventoryList
     *@Description: 外购件出库根据条件查询库存信息
     *@Author: 徐一贺
     *@CreateDate: 2020/3/24 16:49
     *@Phone: 18241927380
     *@Param: [param]
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version:
     */
    public JsonResult findInventoryList(JStorageInventory param) {
        startPage();
        List<JStorageInventory> inventoryList = storageOutService.findInventoryList(param);
        return jsonResult(inventoryList);
    }


    @PostMapping("/findJStorageInventoryList")
    @ResponseBody
    /**
     *@FunctionName: findPInventoryList
     *@Description: 获取模态框所需要的的库存信息
     *@Author: 徐一贺
     *@CreateDate: 2020/3/30 10:18
     *@Phone: 18241927380
     *@Param: [param]
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version:
     */
    public JsonResult findJStorageInventoryList(@RequestParam(value = "existRowStr") String
                                                 existRowStr, JStorageInventoryQueryModel param) {
        List<JStorageInventory> jStorageInventoryList = JSON.parseArray(existRowStr, JStorageInventory.class);
        jStorageInventoryList.addAll(storageOutService.findInventoryListByIds(param.getItyIds()));
        List<JStorageInventory> regularList = new ArrayList<JStorageInventory>();
        List<JStorageInventory> changeList = new ArrayList<JStorageInventory>();
        List<String> errorList = new ArrayList<>();
        for (JStorageInventory model : jStorageInventoryList) {
            JStorageInventory jStorageInventory = storageOutService.getPInventoryByPrimaryKey(model.getItyId());
            if (jStorageInventory.getItyChangeFlag() == null) {
                regularList.add(jStorageInventory);
            } else {
                changeList.add(jStorageInventory);
            }
        }
        if (regularList.size() > 0 && changeList.size() > 0) {
            errorList.add("常规库存不可与串料库存一起出库！<br>");
            errorList.add("以下是串料库存信息！<br>");
            for (JStorageInventory jStorageInventory : changeList) {
                String ityStockTypeStr = "";
                if (jStorageInventory.getItyStockType().equals(1)) {
                    ityStockTypeStr = "【库存在库】";
                    errorList.add("库存类型为：" + ityStockTypeStr + "。物料描述为：【" + jStorageInventory.getItyMatDesc() + "】的库存是【串料库存】。<br>");
                } else {
                    ityStockTypeStr = "【项目在库】";
                    errorList.add("项目编号为：【" + jStorageInventory.getItyProCode() + "】。库存类型为：" + ityStockTypeStr + "。物料描述为：【" + jStorageInventory.getItyMatDesc() + "】的库存是【串料库存】。<br>");
                }

            }
            errorList.add("以下是非串料库存信息！<br>");
            for (JStorageInventory jStorageInventory : regularList) {
                String ityStockTypeStr = "";
                if (jStorageInventory.getItyStockType().equals(1)) {
                    ityStockTypeStr = "【库存在库】";
                    errorList.add("库存类型为：" + ityStockTypeStr + "。物料描述为：【" + jStorageInventory.getItyMatDesc() + "】的库存是【非串料库存】。<br>");
                } else {
                    ityStockTypeStr = "【项目在库】";
                    errorList.add("项目编号为：【" + jStorageInventory.getItyProCode() + "】。库存类型为：" + ityStockTypeStr + "。物料描述为：【" + jStorageInventory.getItyMatDesc() + "】的库存是【非串料库存】。<br>");
                }
                ;
            }
            return JsonResult.fail(errorList.toString());
        }
        return jsonResult(jStorageInventoryList);
    }

    @PostMapping("/createRequestMaterial")
    @ResponseBody
    /**
     *@FunctionName: createRequestMaterial
     *@Description: 创建领料出库单
     *@Author: 徐一贺
     *@CreateDate: 2020/3/23 10:46
     *@Phone: 18241927380
     *@Param: [pOutOrder] 入库数据
     *@Return: com.zhongwang.mms.common.JsonResult 返回结果
     *@Version: 1.0.0
     */
    public JsonResult createRequestMaterial(@RequestBody JOutStorage jOutStorage) {
        return storageOutService.createRequestMaterial(jOutStorage);
    }
}